package com.das.mechanic_base.mvp.view.create.fragment;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.create.SelectWorkBean;
import com.das.mechanic_base.bean.main.DoneDetectionBean;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.kproduce.roundcorners.RoundRelativeLayout;
import com.kproduce.roundcorners.RoundTextView;

import java.util.ArrayList;
import java.util.List;

public class X3AddServiceFastSelectAdapter extends RecyclerView.Adapter<X3AddServiceFastSelectAdapter.X3AddServiceWorkHolder> {

    private List<DoneDetectionBean> mList;

    private Context mContext;

    public X3AddServiceFastSelectAdapter(Context mContext, List<DoneDetectionBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public X3AddServiceWorkHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new X3AddServiceWorkHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_add_service_fast_select_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull X3AddServiceWorkHolder holder, int position) {

        holder.iv_add.setImageResource(R.mipmap.x3_alone_fast_normal);

        holder.tv_name.setText(mList.get(position).getName());

        holder.itemView.setOnClickListener(view -> {
            String tag = (String) holder.iv_add.getTag();
            if ("selected".equals(tag)){
                holder.iv_add.setImageResource(R.mipmap.x3_alone_fast_normal);
                holder.iv_add.setTag("");
                mList.get(position).isSelect = false;
                changeRoundText(holder.rl_content,holder.tv_name,false);
                selectItem(mList.get(position),false);
            } else {
                holder.iv_add.setImageResource(R.mipmap.x3_alone_fast_select);
                holder.iv_add.setTag("selected");
                mList.get(position).isSelect = true;
                changeRoundText(holder.rl_content,holder.tv_name,true);
                selectItem(mList.get(position),true);
            }
        });

    }

    private void selectItem(DoneDetectionBean bean,boolean isSelect){
        if (iOnSelectWorkList != null){
            iOnSelectWorkList.iOnSelectWork(bean,isSelect);
        }
    }

    private void changeRoundText(RoundRelativeLayout rrl, TextView tv, boolean isSelect) {
        rrl.setBackgroundResource(isSelect ? R.drawable.x3_work_fast_select : R.drawable.x3_work_fast_normal);
        tv.setTextColor(isSelect ? ContextCompat.getColor(mContext,R.color.bg_0077FF) : ContextCompat.getColor(mContext,R.color.c333));
    }

    IOnSelectWorkList iOnSelectWorkList;

    public void setiOnSelectWorkList(IOnSelectWorkList iOnSelectWorkList) {
        this.iOnSelectWorkList = iOnSelectWorkList;
    }

    public interface IOnSelectWorkList {
        void iOnSelectWork(DoneDetectionBean bean,boolean isDel);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class X3AddServiceWorkHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView iv_add;
        View v_padding;
        RoundRelativeLayout rl_content;
        public X3AddServiceWorkHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_add = itemView.findViewById(R.id.iv_add);
            v_padding = itemView.findViewById(R.id.v_padding);
            rl_content = itemView.findViewById(R.id.rl_content);
        }
    }

    public List<DoneDetectionBean> getmList() {
        return mList;
    }
}
